package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.domain.Notice;
import com.itbaizhan.openhis.dto.NoticeDto;
import com.itbaizhan.openhis.service.NoticeService;
import com.itbaizhan.openhis.mapper.NoticeMapper;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author MR
* @description 针对表【sys_notice(通知公告表)】的数据库操作Service实现
* @createDate 2025-07-01 23:22:56
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{
	
	@Autowired
	private NoticeMapper noticeMapper;
	
	/**
	 * 查询通知公告
	 * @param noticeDto
	 * @return
	 */
	@Override
	public DataGridView listNoticeForPage(NoticeDto noticeDto) {
		// 分页
		Page<Notice> page = new Page<>(noticeDto.getPageNum(), noticeDto.getPageSize());
		// 条件
		QueryWrapper<Notice> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(noticeDto.getNoticeTitle()),
				Notice.COL_NOTICE_TITLE,noticeDto.getNoticeTitle());// 标题
		wrapper.like(StringUtils.isNotBlank(noticeDto.getCreateBy()),
				Notice.COL_CREATE_BY,noticeDto.getCreateBy());// 类型
		wrapper.eq(StringUtils.isNotBlank(noticeDto.getStatus()),
				Notice.COL_STATUS,noticeDto.getStatus());// 状态
		wrapper.eq(StringUtils.isNotBlank(noticeDto.getNoticeType()),
				Notice.COL_NOTICE_TYPE,noticeDto.getNoticeType());// 类型
		wrapper.orderByAsc(Notice.COL_CREATE_TIME);// 创建时间降序
		// 使用MyBatis-Plus的分页
		noticeMapper.selectPage(page, wrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加通知公告
	 * @param noticeDto
	 * @return
	 */
	@Override
	public int addNotice(NoticeDto noticeDto) {
		Notice notice = new Notice();
		BeanUtils.copyProperties(noticeDto, notice);
		notice.setCreateBy(noticeDto.getSimpleUser().getUserName());
		notice.setCreateTime(DateUtil.date());
		return noticeMapper.insert(notice);
	}
	
	/**
	 * 修改通知公告
	 * @param noticeDto
	 * @return
	 */
	@Override
	public int updateNotice(NoticeDto noticeDto) {
		Notice notice = new Notice();
		BeanUtils.copyProperties(noticeDto, notice);
		notice.setUpdateBy(noticeDto.getSimpleUser().getUserName());
		notice.setUpdateTime(DateUtil.date());
		return noticeMapper.updateById(notice);
	}
	
	/**
	 * 批量删除通知公告
	 * @param noticeIds
	 * @return
	 */
	@Override
	public int deleteNoticeByIds(Long[] noticeIds) {
		List<Long> ids = Arrays.asList(noticeIds);
		if (ids != null && ids.size() > 0){
			return noticeMapper.deleteBatchIds(ids);
		}else {
			return 0;
		}
	}
	
	/**
	 * 根据id查询通知公告
	 * @param noticeId
	 * @return
	 */
	@Override
	public Notice getOne(Long noticeId) {
		return noticeMapper.selectById(noticeId);
	}
}




